from django.shortcuts import render
from .models import *
from rest_framework.views import APIView
from rest_framework.response import Response
from .ser import *
from django.core.paginator import Paginator
import datetime
import random
# Create your views here.




class Sgoods_API(APIView):
    def get(self,request):
        g = Sgoods.objects.all()
        cpage = request.GET.get('cpage')
        pag = Paginator(g, 2)
        tpage = pag.page(cpage)
        ser = Sgoods_Ser(tpage, many=True)
        num = pag.num_pages
        return Response({"code": 200, 'list': ser.data, 'num': num})

    def delete(self,request):
        id = request.GET.get('id')
        Sgoods.objects.filter(id=id).first().delete()
        return Response({"code":200})

class Sfenlei_API(APIView):
    def get(self,request):
        id = request.GET.get('id')
        s = Sgoods.objects.filter(id=id).first()
        g = Sgoods.objects.filter(fenlei_id=s.fenlei_id).all()
        ser = Sgoods_Ser(g,many=True)
        return Response({"code":200,'list':ser.data})

class Wenzhang_API(APIView):
    def post(self,request):
        name = request.data.get('name')
        password = request.data.get('password')
        w = Wusers.objects.filter(name=name).first()
        if w:
            if w.password == password:
                return Response({"code":200,'userid':w.id,'mes':"登录成功"})
            else:
                return Response({"code":10010,'mes':'密码错误'})
        else:
            return Response({"code": 10010, 'mes': '用户名不存在'})

    def get(self,request):
        w = Wenzhang.objects.all()
        cpage = request.GET.get('cpage')
        pag = Paginator(w,2)
        tpage = pag.page(cpage)
        ser = Wenzhang_Ser(tpage,many=True)
        num = pag.num_pages
        return Response({"code":200,'num':num,'list':ser.data})

class Pl_API(APIView):
    def post(self,request):
        ser = Pl_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({"code":200})
        else:
            print(ser.errors)
            return Response({"code":10010})

    def get(self,request):
        wenzhang_id = request.GET.get('wenzhang_id')
        pl = Pl.objects.filter(wenzhang_id=wenzhang_id).all()
        ser = Pl_Ser1(pl,many=True)
        return Response({"code":200,"list":ser.data})




class Juese_API(APIView):
    def get(self,request):
        j = Juese.objects.all()
        ser = Juese_Ser(j,many=True)
        return Response({"code":200,"list":ser.data})

class Ziyuan_API(APIView):
    def get(self,request):
        z = Ziyuan.objects.all()
        ser = Ziyuan_Ser(z,many=True)
        return Response({"code": 200, "list": ser.data})

    def post(self,request):
        ziyuan_id =request.data.get('ziyuan')
        juese_id = request.data.get("juese")
        j = Jue_Zi.objects.filter(juese_id=juese_id).first()
        if j:
            Jue_Zi.objects.filter(juese_id=juese_id).delete()
        for i in ziyuan_id:
            Jue_Zi.objects.create(juese_id=juese_id,ziyuan_id=i).save()
        return Response({"code": 200})


#登录
class Ausers_API(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        w = Ausers.objects.filter(username=username).first()
        if w:
            if w.password == password:
                return Response({"code":200,'userid':w.id,'mes':"登录成功"})
            else:
                return Response({"code":10010,'mes':'密码错误'})
        else:
            return Response({"code": 10010, 'mes': '用户名不存在'})

    def get(self,request):
        a = Anews.objects.all()
        ser = Anews_Ser(a,many=True)
        return Response({"code": 200, "list": ser.data})

import time
#新闻
class Anews_API(APIView):
    def post(self,request):
        news_id = request.data.get('news')
        ser = Apl_Ser(data=request.data)
        if ser.is_valid():
            ser.save()
            a = Anews.objects.filter(id=news_id).first()
            a.count += 1
            a.save()
            return Response({"code":200})
        else:
            return Response({"code":10010})

    def get(self,request):
        news_id = request.GET.get('news')
        userid = request.GET.get('userid')
        a = Anews.objects.filter(id=news_id).first()
        ser = Anews_Ser(a)
        at = Atime.objects.filter(user_id=userid,news_id=news_id).first()
        if not at:
            t = time.time()
            Atime.objects.create(time=str(t),news_id=news_id,user_id=userid).save()
            a.liucount += 1
            a.save()

        else:
            stime = float(time.time())
            xx = Atime.objects.filter(news_id=news_id, user_id=userid).first()
            if (stime - float(xx.time)) > 3600:
                a.liucount += 1
                a.save()
                ss = time.time()
                Atime.objects.filter(news_id=news_id, user_id=userid).update(time=str(ss))

        return Response({"code": 200, "list": ser.data})





# 电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台电商平台

class Ugoods_API(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        w = Users.objects.filter(username=username).first()
        if w:
            if w.password == password:
                return Response({"code": 200, 'userid': w.id, 'mes': "登录成功"})
            else:
                return Response({"code": 10010, 'mes': '密码错误'})
        else:
            return Response({"code": 10010, 'mes': '用户名不存在'})

    def get(self,request):
        u = Ugoods.objects.all()
        ser = Ugoods_Ser(u,many=True)
        return Response({"code":200,"list":ser.data})


class UorderList_API(APIView):
    def post(self,request):
        user = request.data.get('user')
        goods = request.data.get('goods')
        # 生成订单
        order_no = datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S') + str(user) + str(
            random.randint(10000, 99999))
        # #获取当前时间
        x = str(datetime.datetime.now())
        u = Ugoods.objects.filter(id=goods).first()
        UorderList.objects.create(user_id=user,goods_id=goods,price=u.price,Ordernum=order_no,btime=x).save()
        return Response({"code":200})

    def get(self,request):
        user = request.GET.get('userid')
        u = UorderList.objects.filter(user_id=user).all()
        ser = UorderList_Ser(u,many=True)
        return Response({"code":200,'list':ser.data})

    def put(self,request):
        user = int(request.GET.get('userid'))
        id = int(request.GET.get('id'))
        UorderList.objects.filter(id=id).update(state=2,serialNumber='12312312312123')
        #查用户地址
        u = Users.objects.filter(id=1).first()
        #查商品地址
        uor = UorderList.objects.filter(id=id).first()
        goodsa = Ugoods.objects.filter(id=uor.goods_id).first()
        #加入物流订单表
        #生产订单号
        order_no = datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M%S')+ str(user) + str(
            random.randint(10000, 99999))
        OrderList.objects.create(Ordernum=order_no,origin=goodsa.site,destination=u.site,consignee_id=user,price=goodsa.price)
        return Response({"code":200})







































